package com.example.robertluozizhao.framecollectdemo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by robert.luozizhao on 2018/1/31.
 */

public class StepNumberView extends View{
    private Paint mPaint;
    private float startAngle = 110;
    private float sweepAngle = 320;
    private int mMaxStep = 10000;
    private int mCurrentStep = 2000;

    public StepNumberView(Context context) {
        this(context,null);
    }

    public StepNumberView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }


/*    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        ViewGroup.LayoutParams layoutParams = getLayoutParams();
        int w = MeasureSpec.makeMeasureSpec(layoutParams.width, MeasureSpec.EXACTLY);
        int h = MeasureSpec.makeMeasureSpec(layoutParams.height, MeasureSpec.EXACTLY);
        super.onMeasure(w, h);
    }*/

    @Override
    protected void onDraw(Canvas canvas) {

        // 1.画背景大圆弧
        int centerX =  this.getMeasuredWidth()/2;
        int centerY =  this.getMeasuredHeight()/2;
        int roundWidth = 20;
        int radius = centerX - roundWidth;

        mPaint = new Paint();
        //painit 样式
        mPaint.setColor(Color.BLUE);
        mPaint.setStrokeWidth(roundWidth);
        mPaint.setStyle(Paint.Style.STROKE);

        // 设置为 ROUND
        mPaint.setStrokeCap(Paint.Cap.ROUND); //圆角
        mPaint.setStrokeJoin(Paint.Join.ROUND);//??

        RectF oval = new RectF(centerX - radius, centerY - radius, centerX + radius, centerY + radius);
        // 背景圆弧
        canvas.drawArc(oval,startAngle,sweepAngle,false,mPaint);

        //2.进度
        float createAngle = mCurrentStep * (sweepAngle - startAngle) / mMaxStep;
        mPaint.setColor(Color.GREEN);
        canvas.drawArc(oval,startAngle,createAngle,false,mPaint);

        //3.设置文字
        mPaint.reset();
        mPaint.setAntiAlias(true);
        mPaint.setTextSize(60);
        mPaint.setColor(Color.GREEN);

        String text = mCurrentStep+"";
        Rect textBounds = new Rect();
        mPaint.getTextBounds(text,0,text.length(),textBounds);

        float x = (centerX*2 - textBounds.width())/2;
        Paint.FontMetrics fontMetrics = mPaint.getFontMetrics(); // FontMetrics 属性  http://blog.csdn.net/superbigcupid/article/details/47153139
        float y = centerY+((fontMetrics.bottom-fontMetrics.top)/2-fontMetrics.bottom); // 理解 baseline为基准 top ascent ,descent buttom 之间关系

        canvas.drawText(text,x,y,mPaint);


    }
}
